package com.marmot.cloud.framework.core.exception;

import com.marmot.cloud.framework.core.constant.C;
import com.marmot.cloud.framework.core.response.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * 全局异常处理类，用于统一处理应用程序中抛出的异常。
 */
@Slf4j
@ControllerAdvice
class GlobalExceptionHandler {

    /**
     * 处理方法参数校验异常
     */
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    @ResponseBody
    R methodArgumentNotValidException(HttpServletRequest req, MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException : {}", e.getMessage());
        e.printStackTrace();
        StringBuilder sb = new StringBuilder();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        for (ObjectError allError : allErrors) {
            sb.append(allError.getDefaultMessage());
        }
        return R.error(sb.toString());
    }

    /**
     * 处理请求方法不支持异常
     */
    @ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    R httpRequestMethodNotSupportedException(HttpServletRequest req, HttpRequestMethodNotSupportedException e) {
        log.error("HttpRequestMethodNotSupportedException : {}", e.getMessage());
        e.printStackTrace();
        return R.error(C.METHOD_NOT_SUPPORTED);
    }

    /**
     * 处理请求消息不可读异常
     */
    @ExceptionHandler(value = {HttpMessageNotReadableException.class})
    @ResponseBody
    R httpMessageNotReadableException(HttpServletRequest req, HttpMessageNotReadableException e) {
        log.error("HttpRequestMethodNotSupportedException : {}", e.getMessage());
        e.printStackTrace();
        return R.error(C.MSG_NOT_READABLE);
    }

    /**
     * 处理元素不存在异常
     */
    @ExceptionHandler(value = {NoSuchElementException.class})
    @ResponseBody
    R exceptionHandler(HttpServletRequest req, NoSuchElementException e) {
        log.error("NoSuchElementException : {}", e.getMessage());
        e.printStackTrace();
        return R.error(C.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(value = {NullPointerException.class})
    @ResponseBody
    R exceptionHandler(HttpServletRequest req, NullPointerException e) {
        log.error("NullPointerException : {}", e.getMessage());
        e.printStackTrace();
        return R.error(C.PARAM_MISS);
    }

    /**
     * 处理其他异常
     */
//    @ExceptionHandler(value = {Exception.class})
//    @ResponseBody
//    R exceptionHandler(HttpServletRequest req, Exception e) {
//        log.error("Exception : {}", e.getMessage());
//        e.printStackTrace();
//        return R.error(C.INTERNAL_SERVER_ERROR);
//    }

}
